package com.distribution.system.controller;

import com.alibaba.fastjson2.JSONArray;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.utils.StringUtils;
import com.distribution.common.utils.ip.IpUtils;
import com.distribution.system.domain.entity.AddressSystem;
import com.distribution.system.domain.req.AreaCodeOneReq;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.domain.vo.CityVO;
import com.distribution.system.domain.vo.TableArea;
import com.distribution.system.service.AddressSystemService;
import com.distribution.system.service.CmccDaProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping(value = "/addressSystem")
@Slf4j
public class AddressSystemController extends BaseController {
    @Resource
    AddressSystemService addressSystemService;

    @PostMapping("/getProvinceList")
    public AjaxResult getProvinceList(@RequestBody String productCode) {
        Map<String,String> tableArea = addressSystemService.getProvinceMap(productCode);
        //System.out.println("省列表" + tableArea);
        return success(tableArea);
    }

    @PostMapping("/getCityList")
    public AjaxResult getCityList(@RequestBody String productCode) {
        List<CityVO> tableArea = addressSystemService.getCityList(productCode);
        //System.out.println("市列表" + tableArea);
        return success(tableArea);
    }

    @PostMapping("/getPickerAreaMap")
    public AjaxResult getPickerAreaMap(@RequestBody String productCode) {
        JSONArray areaJson = addressSystemService.getPickerAreaMap(productCode);
        //System.out.println(areaJson);
        return success(areaJson);
    }

    @GetMapping("/getAddressByIp")
    public ReqResult getAddressByIp() {
//        String addressStr = IpUtils.getIpAddressByOnline(IpUtils.getIpAddr());
        String addressStr = IpUtils.getIpAddressByOnline("60.27.22.21");
        if (Objects.isNull(addressStr)) return ReqResult.error("未能根据ip找到地区信息");
        String[] split = addressStr.split("\\|");
        String country = split[0];
        String prov = split[1];
        String city = split[2];
        String district = split[3];
        List<AddressSystem> addressSystemList = addressSystemService.lambdaQuery()
                .likeRight(AddressSystem::getProvinceName, prov.substring(0, 2))
                .likeRight(AddressSystem::getCityName, city.substring(0, 2))
                .likeRight(AddressSystem::getDistrictName, district.substring(0, 2)).list();
        if (addressSystemList.isEmpty()) return ReqResult.error("未能根据ip找到地区信息");
        return ReqResult.success(addressSystemList.get(0));
    }

    @PostMapping("/getSystemAddressList")
    public AjaxResult getSystemAddressList() {
        List<TableArea> tableAreas = addressSystemService.getSystemAddressList();
        return success(tableAreas);
    }

    @PostMapping("/getCountyCodeByName")
    public AjaxResult getCountyCodeByName(@RequestBody @Valid AreaCodeOneReq areaCodeOneReq) {
        List<AddressSystem> ysAreaTotalList = addressSystemService.lambdaQuery()
                .like(!StringUtils.isBlank(areaCodeOneReq.getProvinceName()), AddressSystem::getProvinceCode, areaCodeOneReq.getProvinceName() + "%")
                .like(!StringUtils.isBlank(areaCodeOneReq.getCityName()), AddressSystem::getCityCode, areaCodeOneReq.getCityName() + "%")
                .like(!StringUtils.isBlank(areaCodeOneReq.getCountyName()), AddressSystem::getDistrictCode, areaCodeOneReq.getCountyName() + "%")
                .list();
        if (ysAreaTotalList.isEmpty() || ysAreaTotalList.get(0) == null){
            return error("暂无该地区地址信息");
        }
        return success(ysAreaTotalList.get(0).getDistrictCode());
    }
}
